// pages/mine/history/history.js
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    barTitle: '',
    results: [],
    count: 10,
    isNoneShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '正在加载中..'
    })
    let title = options.title
    this.setData({
      barTitle: title
    })
    app.request("get_record", {}, true).then((data) => {
      console.log(data)
      this.processData(data.record.reverse())
      wx.hideLoading()
    })
  },
  processData(data) {
    let len = this.data.count
    if (data.length <= len) {
      this.setData({
        results: data
      })
    } else {
      let record = data.slice(0, len)
      console.log(record)
      this.setData({
        results: record
      })
    }

  },
  navToDetail(e) {
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.name
    wx.navigateTo({
      url: `../../divination/divination-detail/divination-detail?resultId=${id}&name=${name}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.setNavigationBarTitle({
      title: "算卦历史"
    })
  },
  close(e) {
    let id = e.currentTarget.dataset.id
    let that = this
    wx.showModal({
      title: '提示',
      content: '确认删除本条算卦历史？',
      success: function (res) {
        if (res.confirm) {
          app.request("delete_reports", { rep_id: id }).then((data) => {
            app.get_token().then((data) => {
              let token = data
              app.request("get_record", {
                token: token
              }).then((data) => {
                that.setData({
                  results: data.record
                })
              })
            })
          })
        } else if (res.cancel) {
          wx.showToast({
            title: '删除失败',
            duration: 1500
          })
        }
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // if(this.data.results.length <= this.data.count) return
    wx.showNavigationBarLoading()
    let count = this.data.count + 10
    this.setData({
      count: count
    })
    app.request("get_record", {}, true).then((data) => {
      console.log(data)
      this.processData(data.record.reverse())
      if (data.record.length < count) {
        this.setData({
          isNoneShow: true
        })
      }
      wx.hideNavigationBarLoading()
    })

  }

})